<?php
class Admin_Model_Subscriber
{    
    private static $_instance;
    private static $_table = "subscriber";
    
    private function __construct() {
    }
    
    /**
     * @return Admin_Model_Subscriber
     * */
    public static function factory() {
    	if(self::$_instance == null) {
    		self::$_instance = new self();
    	}
    
    	return self::$_instance;
    }
    
    /**
     * Update subscriber
     * @param integer $id
     * @param array $data
     * @return true on success, false on failure
    */
    public function update($id, $data) {
        $result = 0;
        $dao = Admin_Model_DAO_Subscriber::factory();
        try {
        	$result = $dao->update($id, $data);
        	if($result) {//Log
        		$data['id'] = $id;
        		Utils_Global::storeBackendLog('update', self::$_table, $id, $data);
        	}
        } catch (Exception $exc) {
        	prBinh($exc);
        	Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        return $result;
    }
    
    /**
     * Delete article by given $id
     * @param integer $id
     * @return true on success, false on failure
     * */
    public function delete($id) {
        $result = 0;
        if(!$id) {
            return 0;
        }
        
        $dao = Admin_Model_DAO_Subscriber::factory();
        try {
            $result = $dao->delete($id);
            if($result) {//Log
            	Utils_Global::storeBackendLog('delete', self::$_table, $id, array('id' => $id));
            }
        } catch (Exception $exc) {
            prBinh($exc);
            Utils_Global::storeLog($exc, __FILE__, __LINE__);  
        }
        
        return $result;
    }
    
    /**
     * Get subscriber by given conditions
     * @param array $options
     * @return array
     * */
    public function select($options = array()) {
        $result = array();
        $dao = Admin_Model_DAO_Subscriber::factory();
        try {
            $result = $dao->select($options);
        } catch (Exception $exc) {
            prBinh($exc);
            Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        
        return $result;
    }
    
    /**
     * Get subscriber count by given Id
     * @param array $options
     * @return integer
     * */
    public function count($options = array()) {
        $count = 0;
        $dao = Admin_Model_DAO_Subscriber::factory();
        try {
            $count = $dao->count($options);
        } catch (Exception $exc) {
            prBinh($exc);
            Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        
        return $count;
    }
    
    private function isValidArticle($data, $isUpdate=false) {
        if(!$data['i18n_title'] ||
            !$data['i18n_short_description'] ||
             !$data['i18n_content']||
            !$data['category'] ||
            ($isUpdate?false:!$data['hash_folder'])||
            ($isUpdate?false:!$data['image'])) {
            return false;
        }
        
        return true;
    }
    
    /**
     * Select news letter
     * @param array $options
     * @return array
     * */
    public function selectNewsLetter($options = array()) {
        $result = array();
        try {
            $dao = Admin_Model_DAO_Subscriber::factory();
            $result = $dao->selectNewsLetter($options);            
        } catch (Exception $exc) {
            var_dump($exc->getMessage());
            prBinh($exc);
        }
        
        return $result;
    }
    
    /**
     * Delete newsletter
     * @param integer $id
     * @return boolean
     * */
    public function deleteNewsLetter($id) {
        $result = 0;
        try {
        	$dao = Admin_Model_DAO_Subscriber::factory();
        	$result = $dao->deleteNewsLetter($id);
        } catch (Exception $exc) {
        	var_dump($exc->getMessage());
        	prBinh($exc);
        }
        
        return $result;
    }
    
    /**
     * Insert news letter with given data
     * @param array $data
     * @return integer
     * */
    public function insertNewsLetter($data = array()) {
        $result = 0;
        try {
            $dao = Admin_Model_DAO_Subscriber::factory();
            $result = $dao->insertNewsLetter($data);
        } catch (Exception $exc) {
            prBinh($exc);
        }
        return $result;
    }
    
    /**
     * Update news letter by given id & new data
     * @param integer $id
     * @package array $data
     * @return boolean
     * */
    public function updateNewsLetter($id, $data = array()) {
        $result = 0;
        try {
            $dao = Admin_Model_DAO_Subscriber::factory();
            $result = $dao->updateNewsLetter($id, $data);
        } catch (Exception $exc) {
            prBinh($exc);
        }
        return $result;
    }
    
    /**
     * Select news letter count
     * @param array $options
     * @return integer
     * */
    public function countNewsLetter($options) {
        $count = 0;
        $dao = Admin_Model_DAO_Subscriber::factory();
        try {
        	$count = $dao->countNewsLetter($options);
        } catch (Exception $exc) {
        	prBinh($exc);
        	Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        
        return $count;
    }
}